package src; 

import javax.swing.*;
import java.awt.*;

/**
 * The graphical representation of the board. 
 * @author Akshat
 *
 */
public class GUI_Board extends JPanel
{
	private Board board; 
	private GUI_SubGrid[][] grids; 
	private SuperTicTacToeController controller; 
	
	public GUI_Board(Board b, SuperTicTacToeController cn)
	{
		board = b; 
		controller = cn; 
		
		grids = new GUI_SubGrid[3][3]; 
				
		JPanel main = new JPanel(); 
		main.setLayout( new GridBagLayout() );
		main.setBackground(Color.WHITE);
		main.setVisible(true);
		add(main, BorderLayout.CENTER);
		
		int panelWidth = 100; 
		int panelHeight = 100; 
		
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(20,20,20,20);
				
		grids[0][0] = new GUI_SubGrid(board.get(0, 0), controller); 
		grids[0][0].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[0][0].setBackground(Color.WHITE);
		grids[0][0].setVisible(true);
		
		c.gridx = 0;
		c.gridy = 0;
		main.add(grids[0][0], c);		
		main.setVisible(true);

		
		grids[0][1] = new GUI_SubGrid(board.get(0,1), controller); 
		grids[0][1].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[0][1].setBackground(Color.WHITE);
		grids[0][1].setVisible(true);
		
		c.gridx = 1;
		c.gridy = 0; 
		main.add(grids[0][1], c);
		
		
		grids[0][2] = new GUI_SubGrid(board.get(0,2), controller); 
		grids[0][2].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[0][2].setBackground(Color.WHITE);
		grids[0][2].setVisible(true);
		
		c.gridx = 2;
		c.gridy = 0; 
		main.add(grids[0][2], c);
		

		grids[1][0] = new GUI_SubGrid(board.get(1, 0), controller); 
		grids[1][0].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[1][0].setBackground(Color.WHITE);
		grids[1][0].setVisible(true);
		
		c.gridx = 0;
		c.gridy = 1; 
		main.add(grids[1][0], c);
		

		grids[1][1] = new GUI_SubGrid(board.get(1, 1), controller); 
		grids[1][1].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[1][1].setBackground(Color.WHITE);
		grids[1][1].setVisible(true);
		
		c.gridx = 1;
		c.gridy = 1; 
		main.add(grids[1][1], c);
		

		grids[1][2] = new GUI_SubGrid(board.get(1,2), controller); 
		grids[1][2].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[1][2].setBackground(Color.WHITE);
		grids[1][2].setVisible(true);
		
		c.gridx = 2;
		c.gridy = 1; 
		main.add(grids[1][2], c);
		
	
		grids[2][0] = new GUI_SubGrid(board.get(2,0), controller); 
		grids[2][0].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[2][0].setBackground(Color.WHITE);
		grids[2][0].setVisible(true);
		
		c.gridx = 0;
		c.gridy = 2; 
		main.add(grids[2][0], c);
		

		grids[2][1] = new GUI_SubGrid(board.get(2, 1), controller); 
		grids[2][1].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[2][1].setBackground(Color.WHITE);
		grids[2][1].setVisible(true);
		
		c.gridx = 1;
		c.gridy = 2; 
		main.add(grids[2][1], c);
		
				
		grids[2][2] = new GUI_SubGrid(board.get(2,2), controller); 
		grids[2][2].setPreferredSize(new Dimension(panelWidth, panelHeight));
		grids[2][2].setBackground(Color.WHITE);
		grids[2][2].setVisible(true);
		
		c.gridx = 2;
		c.gridy = 2; 
		main.add(grids[2][2], c);
				
		
		
	}

	/**
	 * @param row main row 
	 * @param col main column
	 * @return GUI_SubGrid at that location
	 */
	public GUI_SubGrid getGrid(int row, int col)
	{
		return grids[row][col]; 
	}
	
	/**
	 * Repaints the grid at that location
	 * @param row main row
	 * @param col main col
	 */
	public void paintGrid(int row, int col)
	{
		grids[row][col].draw(grids[row][col].getGraphics());
	}
	
	/**
	 * Repaints all grids on the board
	 */
	public void paintAllGrids()
	{
		for( int i = 0; i < grids.length; i++ )
		{
			for( int j = 0; j < grids[i].length; j++ )
			{
				paintGrid(i,j);
			}
		}
	}

}
